const db = wx.cloud.database()
Page({
  data: {
    order: {
      startTime: '08:00',
      endTime: '11:00',
      title: ''
    },

  },

  onLoad: function(options) {
    var roomID = options.roomID
    var selectDate = options.selectDate
    var order = this.data.order
    order.roomID = roomID
    order.selectDate = selectDate
    var user = wx.getStorageSync('user');
    order.name = user.name
    order.phone_cornet = user.phone_cornet
    order.celphone = user.celphone
    order.numberID = user.student_number
    var userInfo = wx.getStorageSync('userInfo');
    order.avatarUrl = userInfo.avatarUrl

    db.collection('room').where({
      roomID: roomID
    }).get().then(res => {
      var result = res.data[0]
      order.capacity = result.capacity
      order.adminname = result.adminname
      order.adminphone = result.adminphone
      order.adminphone_cornet = result.adminphone_cornet
      order.url = result.url
      this.setData({
        order: order
      })
    })
  },

  updateValue: function(event) {
    let name = event.currentTarget.dataset.name;
    let order = this.data.order
    order[name] = event.detail.value
    this.setData({
      order: order
    })
  },


  TimeChange(event) {
    let name = event.currentTarget.dataset.name;
    var order = this.data.order
    order[name] = event.detail.value
    this.setData({
      order: order
    })
  },

  submitform: function(event) {
    var order = this.data.order
    var order = this.data.order
    var roomID = order.roomID
    var selectDate = order.selectDate
    var status = 1
    if (order.title == '') {
      wx.showToast({
        title: "主题不能为空！",
        icon: 'success',
        duration: 2000
      })
    } else {
      if (order.startTime >= order.endTime) {
        wx.showToast({
          title: "结束时间不对！",
          icon: 'success',
          duration: 2000
        })
      } else {
        db.collection('order').where({
          roomID: roomID,
          selectDate: selectDate
        }).get().then(res => {
          //console.log(res.data)
          var data = res.data
          for (var i = 0; i < data.length; i++) {
            if (data[i].endTime < order.startTime || order.endTime < data[i].startTime) {
              console.log("没有冲突");
            } else {
              status = 0
            }
          }
          if (status == 1) {
            const date = new Date();
            const year = date.getFullYear()
            const month = date.getMonth() + 1
            const day = date.getDate()
            const hour = date.getHours()
            const minute = date.getMinutes()
            const second = date.getSeconds()
            var datastring = year + '年' + month + '月' + day + '日 ' + hour + '时' + minute + '分' + second + '秒';
            order.submitdate = datastring
            db.collection('order').add({
              data: order
            })
              .then(res => {
                console.log(res)
                wx.navigateBack({
                  delta: 1
                })

              })
          }
          else
          {
            wx.showToast({
              title: "会议时间冲突！",
              icon: 'success',
              duration: 2000
            })
          }

        })
      }
    }

  }


})